package com.liang.lingdong.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liang.lingdong.common.ErrorCode;
import com.liang.lingdong.exception.BusinessException;
import com.liang.lingdong.model.exam.Exam;
import com.liang.lingdong.model.sign.Sign;
import com.liang.lingdong.model.signTopic.SignTopic;
import com.liang.lingdong.model.signTopic.dto.UserSubmitDto;
import com.liang.lingdong.model.signTopic.vo.SignTopicVo;
import com.liang.lingdong.model.topic.Topic;
import com.liang.lingdong.service.ExamService;
import com.liang.lingdong.service.SignService;
import com.liang.lingdong.service.SignTopicService;
import com.liang.lingdong.mapper.SignTopicMapper;
import com.liang.lingdong.service.TopicService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author liang
* @description 针对表【sign_topic(做题表)】的数据库操作Service实现
* @createDate 2024-11-29 21:34:17
*/
@Service
public class SignTopicServiceImpl extends ServiceImpl<SignTopicMapper, SignTopic>
    implements SignTopicService{

    @Resource
    private SignService signService;

    @Resource
    private ExamService examService;

    @Resource
    private TopicService topicService;

    /**
     * 用户提交答案
     * @param userSubmitDto 用户提交答案
     * @return 结果
     */
    @Override
    public boolean userSubmit(UserSubmitDto userSubmitDto) {
        // 获取登录id
        Long signId = userSubmitDto.getSignId();
        // 查询报名内容
        Sign sign = signService.lambdaQuery()
                .eq(Sign::getBusinessId, signId)
                .one();
        if (sign == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"未报名该考试");
        }
        // 判断考试时间
        Long examId = sign.getExamId();
        Exam exam = examService.getById(examId);
        if (new Date().before(exam.getStartTime()) || new Date().after(exam.getEndTime())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"考试未开始或已结束");
        }
        // 判断本题有没有提交过
        SignTopic signTopic = lambdaQuery()
                .eq(SignTopic::getSignId, signId)
                .eq(SignTopic::getTopicId, userSubmitDto.getTopicId())
                .one();
        if (signTopic != null) {
            signTopic.setAnswer(userSubmitDto.getAnswer());
            // 有记录则更新答案
            boolean update = updateById(signTopic);
            if (!update) {
                throw new BusinessException(ErrorCode.OPERATION_ERROR);
            }
        } else {
            signTopic = new SignTopic();
            signTopic.setSignId(signId);
            signTopic.setTopicId(userSubmitDto.getTopicId());
            signTopic.setAnswer(userSubmitDto.getAnswer());
            signTopic.setExamId(sign.getExamId());
            signTopic.setSubmitTime(new Date());
            // 如果没有记录则插入
            boolean save = save(signTopic);
            if (!save) {
                throw new BusinessException(ErrorCode.OPERATION_ERROR);
            }
        }
        return true;
    }

    /**
     * 获取判题题状态
     * @param examId 考试id
     * @return 做题状态
     */
    @Override
    public List<SignTopic> getStatus(Long examId, Long topicId) {
        return lambdaQuery()
                .eq(SignTopic::getExamId, examId)
                .eq(SignTopic::getTopicId, topicId)
                .list();
    }

    /**
     * 获取我的答案
     * @param signId 报名id
     * @return 我的答案
     */
    @Override
    public List<SignTopicVo> getMyAnswer(Long signId) {
        // 查询报名内容
        Sign sign = signService.lambdaQuery()
                .eq(Sign::getBusinessId, signId)
                .one();
        if (sign == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"未报名该考试");
        }
        // 查询比赛
        Exam exam = examService.getById(sign.getExamId());
        if (new Date().before(exam.getStartTime()) || new Date().after(exam.getEndTime())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"考试未开始或已结束");
        }
        // 查询比赛下所有题目
        List<Topic> topicList = topicService.lambdaQuery()
                .eq(Topic::getExamId, sign.getExamId())
                .list();
        return topicList.stream().map(topic -> {
            // 查询报名下的该题目
            SignTopic signTopic = lambdaQuery()
                    .eq(SignTopic::getSignId, signId)
                    .eq(SignTopic::getTopicId, topic.getBusinessId())
                    .one();

            return SignTopicVo.builder()
                    .businessId(signTopic.getBusinessId())
                    .answer(signTopic.getAnswer())
                    .signId(signId)
                    .topicId(topic.getBusinessId())
                    .examId(sign.getExamId())
                    .submitTime(signTopic.getSubmitTime())
                    .build();
        }).collect(Collectors.toList());
    }
}




